home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 118 / cd-rom 118.iso / aplic / open / openofficeorg1.cab / sbasic.jar / text / sbasic / shared / 03090202.xhp < prev    next >
Encoding:
Extensible Markup Language  |  2005-02-14  |  11.5 KB  |  137 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--***********************************************************************
  3.  *
  4.  *  The Contents of this file are made available subject to the terms of
  5.  *  either of the following licenses
  6.  *
  7.  *         - GNU Lesser General Public License Version 2.1
  8.  *         - Sun Industry Standards Source License Version 1.1
  9.  *
  10.  *  Sun Microsystems Inc., October, 2000
  11.  *
  12.  *  GNU Lesser General Public License Version 2.1
  13.  *  =============================================
  14.  *  Copyright 2000 by Sun Microsystems, Inc.
  15.  *  901 San Antonio Road, Palo Alto, CA 94303, USA
  16.  *
  17.  *  This library is free software; you can redistribute it and/or
  18.  *  modify it under the terms of the GNU Lesser General Public
  19.  *  License version 2.1, as published by the Free Software Foundation.
  20.  *
  21.  *  This library is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  24.  *  Lesser General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU Lesser General Public
  27.  *  License along with this library; if not, write to the Free Software
  28.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29.  *  MA  02111-1307  USA
  30.  *
  31.  *
  32.  *  Sun Industry Standards Source License Version 1.1
  33.  *  =================================================
  34.  *  The contents of this file are subject to the Sun Industry Standards
  35.  *  Source License Version 1.1 (the "License"); You may not use this file
  36.  *  except in compliance with the License. You may obtain a copy of the
  37.  *  License at http://www.openoffice.org/license.html.
  38.  *
  39.  *  Software provided under this License is provided on an "AS IS" basis,
  40.  *  WITHOUT WARRUNTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING,
  41.  *  WITHOUT LIMITATION, WARRUNTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
  42.  *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
  43.  *  See the License for the specific provisions governing your rights and
  44.  *  obligations concerning the Software.
  45.  *
  46.  *  The Initial Developer of the Original Code is: Sun Microsystems, Inc..
  47.  *
  48.  *  Copyright: 2000 by Sun Microsystems, Inc.
  49.  *
  50.  *  All Rights Reserved.
  51.  *
  52.  *  Contributor(s): _______________________________________
  53.  *
  54.  *
  55.  ************************************************************************-->
  56.     
  57. <helpdocument version="1.0">
  58. <meta>
  59. <topic id="textsbasicshared03090202xml" indexer="include" status="PUBLISH">
  60. <title id="tit" xml-lang="en-US">For...Next Statement [Runtime]</title>
  61. <filename>/text/sbasic/shared/03090202.xhp</filename>
  62. </topic>
  63. <history>
  64. <created date="2003-10-31T00:00:00">Sun Microsystems, Inc.</created>
  65. <lastedited date="2004-08-24T12:09:02">converted from old format - fpe</lastedited>
  66. </history>
  67. </meta>
  68. <body>
  69. <section id="fornext">
  70. <bookmark xml-lang="en-US" branch="index" id="bm_id3149205"><bookmark_value>For statement</bookmark_value>
  71. <bookmark_value>To statement</bookmark_value>
  72. <bookmark_value>Step statement</bookmark_value>
  73. <bookmark_value>Next statement</bookmark_value>
  74. </bookmark>
  75. <paragraph role="heading" id="hd_id3149205" xml-lang="en-US" level="1" l10n="U" oldref="1"><link href="text/sbasic/shared/03090202.xhp" name="For...Next Statement [Runtime]">For...Next Statement [Runtime]</link></paragraph>
  76. <paragraph role="paragraph" id="par_id3143267" xml-lang="en-US" l10n="U" oldref="2">Repeats the statements between the For...Next block a specified number of times.</paragraph>
  77. </section>
  78. <paragraph role="heading" id="hd_id3156153" xml-lang="en-US" level="2" l10n="U" oldref="3">Syntax:</paragraph>
  79. <paragraph role="paragraph" id="par_id3148473" xml-lang="en-US" l10n="U" oldref="4">For counter=start To end [Step step]</paragraph>
  80. <paragraph role="paragraph" id="par_id3156024" xml-lang="en-US" l10n="U" oldref="5">statement block</paragraph>
  81. <paragraph role="paragraph" id="par_id3146796" xml-lang="en-US" l10n="U" oldref="6">[Exit For]</paragraph>
  82. <paragraph role="paragraph" id="par_id3159414" xml-lang="en-US" l10n="U" oldref="7">statement block</paragraph>
  83. <paragraph role="paragraph" id="par_id3153897" xml-lang="en-US" l10n="U" oldref="8">Next [counter]</paragraph>
  84. <paragraph role="heading" id="hd_id3150400" xml-lang="en-US" level="2" l10n="U" oldref="9">Variables:</paragraph>
  85. <paragraph role="paragraph" id="par_id3150358" xml-lang="en-US" l10n="U" oldref="10">
  86. <emph>Counter:</emph> Loop counter initially assigned the value to the right of the equal sign (start). Only numeric variables are valid. The loop counter increases or decreases according to the variable Step until End is passed.</paragraph>
  87. <paragraph role="paragraph" id="par_id3152455" xml-lang="en-US" l10n="U" oldref="11">
  88. <emph>Start:</emph> Numeric variable that defines the initial value at the beginning of the loop.</paragraph>
  89. <paragraph role="paragraph" id="par_id3151043" xml-lang="en-US" l10n="U" oldref="12">
  90. <emph>End:</emph> Numeric variable that defines the final value at the end of the loop.</paragraph>
  91. <paragraph role="paragraph" id="par_id3156281" xml-lang="en-US" l10n="U" oldref="13">
  92. <emph>Step:</emph> Sets the value by which to increase or decrease the loop counter. If Step is not specified, the loop counter is incremented by 1. In this case, End must be greater than Start. If you want to decrease Counter, End must be less than Start, and Step must be assigned a negative value.</paragraph>
  93. <paragraph role="paragraph" id="par_id3154684" xml-lang="en-US" l10n="U" oldref="14">The <emph>For...Next</emph> loop repeats all of the statements in the loop for the number of times that is specified by the parameters.</paragraph>
  94. <paragraph role="paragraph" id="par_id3147287" xml-lang="en-US" l10n="U" oldref="15">As the counter variable is decreased, $[officename] Basic checks if the end value has been reached. As soon as the counter passes the end value, the loop automatically ends.</paragraph>
  95. <paragraph role="paragraph" id="par_id3159154" xml-lang="en-US" l10n="U" oldref="16">It is possible to nest <emph>For...Next</emph> statements. If you do not specify a variable following the <emph>Next</emph> statement, <emph>Next</emph> automatically refers to the most recent <emph>For</emph> statement.</paragraph>
  96. <paragraph role="paragraph" id="par_id3155306" xml-lang="en-US" l10n="U" oldref="17">If you specify an increment of 0, the statements between <emph>For</emph> and <emph>Next</emph> are repeated continuously.</paragraph>
  97. <paragraph role="paragraph" id="par_id3155854" xml-lang="en-US" l10n="U" oldref="18">When counting down the counter variable, $[officename] Basic checks for overflow or underflow. The loop ends when Counter exceeds End (positive Step value) or is less than End (negative Step value).</paragraph>
  98. <paragraph role="paragraph" id="par_id3145273" xml-lang="en-US" l10n="U" oldref="19">Use the <emph>Exit For</emph> statement to exit the loop unconditionally. This statement must be within a <emph>For...Next</emph> loop. Use the <emph>If...Then</emph> statement to test the exit condition as follows:</paragraph>
  99. <paragraph role="paragraph" id="par_id3153190" xml-lang="en-US" l10n="U" oldref="20">For...</paragraph>
  100. <paragraph role="paragraph" id="par_id3149482" xml-lang="en-US" l10n="U" oldref="21">statements</paragraph>
  101. <paragraph role="paragraph" id="par_id3147124" xml-lang="en-US" l10n="U" oldref="22">If condition = True Then Exit For</paragraph>
  102. <paragraph role="paragraph" id="par_id3153159" xml-lang="en-US" l10n="U" oldref="23">statements</paragraph>
  103. <paragraph role="paragraph" id="par_id3154096" xml-lang="en-US" l10n="U" oldref="24">Next</paragraph>
  104. <paragraph role="paragraph" id="par_id3156286" xml-lang="en-US" l10n="U" oldref="25">Note: In nested <emph>For...Next</emph> loops, if you exit a loop unconditionally with <emph>Exit For</emph>, only one loop is exited.</paragraph>
  105. <paragraph role="heading" id="hd_id3148457" xml-lang="en-US" level="2" l10n="U" oldref="26">Example</paragraph>
  106. <paragraph role="paragraph" id="par_id3151074" xml-lang="en-US" l10n="U" oldref="27">The following example uses two nested loops to sort a string array with 10 elements ( sEntry() ), that are first filled with various contents:</paragraph>
  107. <paragraph role="paragraph" id="par_id3155603" xml-lang="en-US" l10n="U" oldref="28">Sub ExampleSort</paragraph>
  108. <paragraph role="paragraph" id="par_id3156275" xml-lang="en-US" l10n="U" oldref="29">Dim sEntry(9) As String</paragraph>
  109. <paragraph role="paragraph" id="par_id3155066" xml-lang="en-US" l10n="U" oldref="30">Dim iCount As Integer</paragraph>
  110. <paragraph role="paragraph" id="par_id3150751" xml-lang="en-US" l10n="U" oldref="31">Dim iCount2 As Integer</paragraph>
  111. <paragraph role="paragraph" id="par_id3155446" xml-lang="en-US" l10n="U" oldref="32">Dim sTemp As String</paragraph>
  112. <paragraph role="paragraph" id="par_id3155767" xml-lang="en-US" l10n="U" oldref="42">sEntry(0) = "Jerry"</paragraph>
  113. <paragraph role="paragraph" id="par_id3153711" xml-lang="en-US" l10n="U" oldref="33">sEntry(1) = "Patty"</paragraph>
  114. <paragraph role="paragraph" id="par_id3148993" xml-lang="en-US" l10n="U" oldref="34">sEntry(2) = "Kurt"</paragraph>
  115. <paragraph role="paragraph" id="par_id3156382" xml-lang="en-US" l10n="U" oldref="35">sEntry(3) = "Thomas"</paragraph>
  116. <paragraph role="paragraph" id="par_id3155174" xml-lang="en-US" l10n="U" oldref="36">sEntry(4) = "Michael"</paragraph>
  117. <paragraph role="paragraph" id="par_id3166448" xml-lang="en-US" l10n="U" oldref="37">sEntry(5) = "David"</paragraph>
  118. <paragraph role="paragraph" id="par_id3149255" xml-lang="en-US" l10n="U" oldref="38">sEntry(6) = "Cathy"</paragraph>
  119. <paragraph role="paragraph" id="par_id3149565" xml-lang="en-US" l10n="U" oldref="39">sEntry(7) = "Susie"</paragraph>
  120. <paragraph role="paragraph" id="par_id3145148" xml-lang="en-US" l10n="U" oldref="40">sEntry(8) = "Edward"</paragraph>
  121. <paragraph role="paragraph" id="par_id3145229" xml-lang="en-US" l10n="U" oldref="41">sEntry(9) = "Christine"</paragraph>
  122. <paragraph role="paragraph" id="par_id3149107" xml-lang="en-US" l10n="U" oldref="44">For iCount = 0 To 9</paragraph>
  123. <paragraph role="paragraph" id="par_id3148485" xml-lang="en-US" l10n="U" oldref="45">For iCount2 = iCount + 1 To 9</paragraph>
  124. <paragraph role="paragraph" id="par_id3155608" xml-lang="en-US" l10n="U" oldref="46">If sEntry(iCount) > sEntry(iCount2) Then</paragraph>
  125. <paragraph role="paragraph" id="par_id3150938" xml-lang="en-US" l10n="U" oldref="47">sTemp = sEntry(iCount)</paragraph>
  126. <paragraph role="paragraph" id="par_id3153790" xml-lang="en-US" l10n="U" oldref="48">sEntry(iCount) = sEntry(iCount2)</paragraph>
  127. <paragraph role="paragraph" id="par_id3149210" xml-lang="en-US" l10n="U" oldref="49">sEntry(iCount2) = sTemp</paragraph>
  128. <paragraph role="paragraph" id="par_id3153781" xml-lang="en-US" l10n="U" oldref="50">End If</paragraph>
  129. <paragraph role="paragraph" id="par_id3158446" xml-lang="en-US" l10n="U" oldref="51">Next iCount2</paragraph>
  130. <paragraph role="paragraph" id="par_id3150783" xml-lang="en-US" l10n="U" oldref="52">Next iCount</paragraph>
  131. <paragraph role="paragraph" id="par_id3151278" xml-lang="en-US" l10n="U" oldref="57">For iCount = 0 To 9</paragraph>
  132. <paragraph role="paragraph" id="par_id3148462" xml-lang="en-US" l10n="U" oldref="58">Print sEntry(iCount)</paragraph>
  133. <paragraph role="paragraph" id="par_id3149528" xml-lang="en-US" l10n="U" oldref="59">Next iCount</paragraph>
  134. <paragraph role="paragraph" id="par_id3152580" xml-lang="en-US" l10n="U" oldref="60">End Sub</paragraph>
  135. </body>
  136. </helpdocument>
  137.